package export.bullet
{
	import base.BaseBullet;
	import base.BaseObject;
	
	public class FollowTargetBullet extends BaseBullet
	{
		
		public function FollowTargetBullet(sourceName:String = "")
		{
			super(sourceName);
			setIsDestroyWhenOwnerHurt(false);
		}
		
		protected var followTarget:BaseObject;
		
		protected var isTrackDirection:Boolean = true;
		
		override public function render(ratio:Number = 1):void
		{
			if(!followTarget)
			{
				followTarget = owner;
			}
			if(followTarget.isDead())
			{
				followTarget = null;
				destroy();
				return;
			}
			//这两个属性不能共存，不然子弹方向就会怪怪的
			if(isTrackDirection && isAutoDirect)
			{
				isAutoDirect = false;
			}
			if(isTrackDirection)
			{
				setDirect(followTarget.getBBDC().getDirect());
			}
			super.render(ratio);
		}
		
		override protected function updatePosition(ratio:Number = 1):void
		{
			if(isReadyToDestroy) return;
			x = followTarget.x;
			y = followTarget.y;
		}
		
		/**
		 * 是否根据跟随目标自动设置方向
		 * @param isAuto
		 */
		public function setTrackDirection(isAuto:Boolean = true):void
		{
			isTrackDirection = isAuto;
		}
		
		override public function setOwner(o:BaseObject):void
		{
			owner = o;
		}
		
		public function setTarget(target:BaseObject):void
		{
			followTarget = target;
		}
		
		override public function destroy():void
		{
			super.destroy();
			owner = null;
			followTarget = null;
		}
	}
}
